package com.mingtu.jgspatrol.activity;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.base.MyBaseActivity;
import com.mingtu.jgspatrol.databinding.ActivityNotificationBinding;
import com.mingtu.jgspatrol.fragment.MsgNoticeFragment;
import com.mingtu.jgspatrol.fragment.UnitNoticeFragment;

import java.util.ArrayList;

public class NotificationActivity extends MyBaseActivity<ActivityNotificationBinding> {
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"单位通知", "消息通知"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private int noticeTypeId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_notification;
    }

    @Override
    protected void initView() {
        setModuleTitle("通知");
        super.initView();
    }

    @Override
    protected void initData() {
        int type = getIntent().getIntExtra("type", 0);
        noticeTypeId = getIntent().getIntExtra("noticeTypeId", 0);
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        UnitNoticeFragment unitNoticeFragment = UnitNoticeFragment.getInstance(noticeTypeId);
        MsgNoticeFragment msgNoticeFragment = MsgNoticeFragment.getInstance();
        mFragments.add(unitNoticeFragment);
        mFragments.add(msgNoticeFragment);


        binding.viewPager.setOffscreenPageLimit(2);//需要设置此值
        binding.viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments));
        binding.viewPager.setCurrentItem(type);
        binding.viewPager.addOnPageChangeListener(new MyOnPageChangeListener());


        binding.commonTab.setTextBold(1);
        binding.commonTab.setTabData(mTabEntities);
        binding.commonTab.setOnTabSelectListener(new MyOnTabSelectListener());
        binding.commonTab.setCurrentTab(type);


    }

    @Override
    protected void getData() {


    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            binding.viewPager.setCurrentItem(position);
        }

        @Override
        public void onTabReselect(int position) {

        }
    }


    private class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            binding.commonTab.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

    }

}